/**
*  \file     li_ion_sdo.h
*
*  \brief    Lithium-Ion battery management system SDO transfer module
*
*  \author   Ralf Huber <ralf.huber@linde-mh.de>
*
*/

#ifndef LI_ION_SDO_H_INCLUDED
#define LI_ION_SDO_H_INCLUDED

/*================================================[ public includes  ]================================================*/
#include "..\cfg\prj.h"

/*================================================[ public defines   ]================================================*/

/*
* Specifies the size of the SDO-transfer transmit/receive buffer. Must be >= the largest SDO object that is sent to or
* received from the li-ion battery SDO server.
*/
#define LI_ION_SDO_MAX_OBJECT_SIZE    15

/*================================================[ public datatypes ]================================================*/

typedef enum
{
   LI_ION_CAN_SCHEME_WAREHOUSE      = 0x13,     /*!< for BR117                                                   */
   LI_ION_CAN_SCHEME_REACH_A        = 0x20,     /*!< for BR115, BR116, BR1123                                    */
   LI_ION_CAN_SCHEME_REACH_B        = 0x21,     /*!< for BR1120                                                  */
   LI_ION_CAN_SCHEME_COUNTERBALANCE = 0x30      /*!< for BR38x and all vehicles with similar CAN message layouts */
} li_ion_sdo_can_scheme_t;

/*
* Function pointer type for Li-Ion SDO post-transfer callbacks
*
* When an SDO transfer is started by using one of the "li_ion_sdo_send/request_xxx" functions in this module, you can
* pass a function of this type, which will then be called after the SDO transfer is completed. The arguments of the
* callback give information about the SDO transfer.
*
* \param sdo_transfer_status  RC_SUCCESS           If the SDO-transfer was completed successfully.                <br/>
*                             RC_ERROR             If the SDO-transfer was aborted (by SDO server or client).     <br/>
*                             RC_ERROR_BUFFER_FULL If the uploaded SDO data was too big for the receive buffer.
* \param sdo_upload_size      Contains the amount of received bytes in case the SDO request was an upload.        <br/>
*                             Is 0 in case the SDO request was a download.
* \param sdo_abort_code       Contains the SDO abort code if sdo_transfer_status == RC_ERROR. (see /cosdo/cosdo_abort_codes.h for all abort codes) <br/>
*                             Is 0 if sdo_transfer_status != RC_ERROR.
*/
typedef void(*li_ion_sdo_callback_t)(RC sdo_transfer_status, U32 sdo_upload_size, U32 sdo_abort_code);

/*================================================[ public variables ]================================================*/

/*================================================[ public functions ]================================================*/
/*
* Lithium-Ion battery management system SDO-transfer initialization
*
* Initialize the Li-Ion-SDO-transfer module.
*/
void li_ion_sdo_initialize(void);

/*
* Li-Ion SDO client callback
*
* This function must be called from the CAN-driver whenever an SDO response arrives from the Li-Ion SDO server for a
* request that was previously sent by the traction controller SDO client.
*
* \param    msg   8-byte buffer that contains the SDO response message content
*/
void li_ion_sdo_client_receive(U8 msg[8]);

/*====================================================================================================================*/
/* SDO downloads */

/*
* This function sends the battery nominal voltage value to the lithium-ion battery management system. The specified
* callback function will be called after the transfer is completed (successfully or not). It is safe to pass NULL for
* the callback if no callback is necessary.
*
* \param    post_transfer_callback  Will be called after the SDO response from the SDO server is processed.
* \param    nominal_voltage         Pointer to nominal battery voltage value that will be sent to the Li-Ion BMS.
* \return   RC_SUCCESS              If the SDO request could be sent successfully.                                      <br/>
*           RC_ERROR_BUSY           If the SDO request could not be sent because another SDO transfer is in progress.   <br/>
*/
RC li_ion_sdo_send_nominal_voltage( li_ion_sdo_callback_t post_transfer_callback,
                                    const U8 nominal_voltage );

/*
* This function sends the CAN-scheme value to the lithium-ion battery management system. The specified callback
* function will be called after the transfer is completed (successfully or not). It is safe to pass NULL for the
* callback if no callback is necessary.
*
* \param    post_transfer_callback  Will be called after the SDO response from the SDO server is processed.
* \param    can_scheme              CAN-scheme value that will be sent to the Li-Ion BMS.
* \return   RC_SUCCESS              If the SDO request could be sent successfully.                                      <br/>
*           RC_ERROR_BUSY           If the SDO request could not be sent because another SDO transfer is in progress.   <br/>
*/
RC li_ion_sdo_send_can_scheme( li_ion_sdo_callback_t post_transfer_callback,
                               const li_ion_sdo_can_scheme_t can_scheme );

/*
* This function sends the vehicle identification number to the lithium-ion battery management system. The specified
* callback function will be called after the transfer is completed (successfully or not). It is safe to pass NULL for
* the callback if no callback is necessary.
*
* \param    post_transfer_callback  Will be called after the SDO response from the SDO server is processed.
* \param    vin                     Pointer to a char array which contains the VIN.
* \param    vin_length              How many bytes of 'vin' should be sent to the Li-Ion BMS.
* \return   RC_SUCCESS              If the SDO request could be sent successfully.                                      <br/>
*           RC_ERROR_NULL           If 'vin' is a NULL pointer.                                                         <br/>
*           RC_ERROR_BUSY           If the SDO request could not be sent because another SDO transfer is in progress.   <br/>
*/
RC li_ion_sdo_send_VIN( li_ion_sdo_callback_t post_transfer_callback,
                        const CH * const vin,
                        const U8 vin_length );

/*
* This function sends an SDO request to turn on the lithium-ion battery main contactor. The specified callback function
* will be called after the transfer is completed (successfully or not). It is safe to pass NULL for the callback if no
* callback is necessary.
*
* \param    post_transfer_callback  Will be called after the SDO response from the SDO server is processed.
* \return   RC_SUCCESS              If the SDO request could be sent successfully.                                      <br/>
*           RC_ERROR_BUSY           If the SDO request could not be sent because another SDO transfer is in progress.   <br/>
*/
RC li_ion_sdo_turn_battery_on(li_ion_sdo_callback_t post_transfer_callback);

/*
* This function sends an SDO request to turn off the lithium-ion battery main contactor. The specified callback
* function will be called after the transfer is completed (successfully or not). It is safe to pass NULL for the
* callback if no callback is necessary.
*
* \param    post_transfer_callback  Will be called after the SDO response from the SDO server is processed.
* \return   RC_SUCCESS              If the SDO request could be sent successfully.                                      <br/>
*           RC_ERROR_BUSY           If the SDO request could not be sent because another SDO transfer is in progress.   <br/>
*/
RC li_ion_sdo_turn_battery_off(li_ion_sdo_callback_t post_transfer_callback);

/*
* This function sends an SDO request to turn off the lithium-ion battery main contactor. The specified callback
* function will be called after the transfer is completed (successfully or not). It is safe to pass NULL for the
* callback if no callback is necessary.
*
* \param    post_transfer_callback  Will be called after the SDO response from the SDO server is processed.
* \return   RC_SUCCESS              If the SDO request could be sent successfully.                                      <br/>
*           RC_ERROR_BUSY           If the SDO request could not be sent because another SDO transfer is in progress.   <br/>
*/
RC li_ion_sdo_reset_battery(li_ion_sdo_callback_t post_transfer_callback);

/*====================================================================================================================*/
/* SDO uploads */

/*
* This function sends an SDO request to upload the battery serial number from the BMS to the traction controller. The
* specified callback function will be called after the transfer is completed (successfully or not). It is safe to pass
* NULL for the callback if no callback is necessary.
*
* \param    post_transfer_callback  Will be called after the SDO response from the SDO server is processed.
* \param    serial_number           This is where the received battery serial number will be stored.
* \return   RC_SUCCESS              If the SDO request could be sent successfully.                                      <br/>
*           RC_ERROR_BUSY           If the SDO request could not be sent because another SDO transfer is in progress.
*/
RC li_ion_sdo_request_battery_serial_number( li_ion_sdo_callback_t post_transfer_callback,
                                             U32 * const serial_number );

/*
* This function sends an SDO request to upload the battery energy content from the BMS to the traction controller. The
* specified callback function will be called after the transfer is completed (successfully or not). It is safe to pass
* NULL for the callback if no callback is necessary.
*
* \param    post_transfer_callback  Will be called after the SDO response from the SDO server is processed.
* \param    energy_content          This is where the received battery energy content will be stored.
* \return   RC_SUCCESS              If the SDO request could be sent successfully.                                      <br/>
*           RC_ERROR_BUSY           If the SDO request could not be sent because another SDO transfer is in progress.
*/
RC li_ion_sdo_request_battery_energy( li_ion_sdo_callback_t post_transfer_callback,
                                      U32 * const energy_content );

/*
* This function sends an SDO request to upload the battery software variant string from the BMS to the traction
* controller. The specified callback function will be called after the transfer is completed (successfully or not). It
* is safe to pass NULL for the callback if no callback is necessary.
*
* \param    post_transfer_callback  Will be called after the SDO response from the SDO server is processed.
* \param    buffer                  This is where the received software variant string will be stored.
* \param    buffer_size             Size of 'buffer' (in bytes).
* \return   RC_SUCCESS              If the SDO request could be sent successfully.                                      <br/>
*           RC_ERROR_BUSY           If the SDO request could not be sent because another SDO transfer is in progress.
*/
RC li_ion_sdo_request_software_variant( li_ion_sdo_callback_t post_transfer_callback,
                                        CH * const buffer,
                                        const U8 buffer_size );

/*
* This function sends an SDO request to upload the battery hardware variant string from the BMS to the traction
* controller. The specified callback function will be called after the transfer is completed (successfully or not). It
* is safe to pass NULL for the callback if no callback is necessary.
*
* \param    post_transfer_callback  Will be called after the SDO response from the SDO server is processed.
* \param    buffer                  This is where the received hardware variant string will be stored.
* \param    buffer_size             Size of 'buffer' (in bytes).
* \return   RC_SUCCESS              If the SDO request could be sent successfully.                                      <br/>
*           RC_ERROR_BUSY           If the SDO request could not be sent because another SDO transfer is in progress.
*/
RC li_ion_sdo_request_hardware_variant( li_ion_sdo_callback_t post_transfer_callback,
                                        CH * const buffer,
                                        const U8 buffer_size );

/*====================================================================================================================*/
#endif
/***********************************************************************************************************************
*                             Copyright 2014 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/
